﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;

namespace Anuntiomatic
{
    public class HTMLHelper
    {
        public static string UA = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36";
        public static object[] GetURLReturnCookieAndString(string url, CookieCollection cookie = null)
        {
            if (cookie == null) return null;
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = UA;
                req.CookieContainer = new CookieContainer();
                req.CookieContainer.Add(cookie);
                using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream str = res.GetResponseStream())
                    {
                        using (StreamReader streader = new StreamReader(str))
                        {
                            return new object[] { req.CookieContainer.GetCookies(new Uri(url)), streader.ReadToEnd() };
                        }
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object[] PostURLReturnCookieAndString(string url, string para, CookieCollection cookie)
        {
            if (cookie == null) return null;
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(para);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.UserAgent = UA;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookie);
                request.Method = "POST";
                request.ContentLength = data.Length;

                request.ContentType = "application/x-www-form-urlencoded";
                using (Stream st = request.GetRequestStream())
                {
                    st.Write(data, 0, data.Length);
                    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        using (Stream str = response.GetResponseStream())
                        {
                            using (StreamReader streader = new StreamReader(str))
                            {
                                return new object[] { request.CookieContainer.GetCookies(new Uri(url)), streader.ReadToEnd(), response.ResponseUri.AbsoluteUri };
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Image GetImage(string url, CookieCollection cookies)
        {
            if (cookies == null) return null;
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = UA;
                req.CookieContainer = new CookieContainer();
                req.CookieContainer.Add(cookies);
                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream str = response.GetResponseStream())
                    {
                        return Image.FromStream(str);
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
